﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Datiotec
{
    class Controlador
    {
        internal PictureBox img_Left, img_Right, img_Up, img_Down, img_Power, img_Uno, img_Dos, img_Tres;
        internal PictureBox img_Dia, img_DisplayH1, img_DisplayH2, img_Puntos, img_DisplayM1, img_DisplayM2;
        private DateTime fechaHoraActual;
        private bool edo_Encendido, edo_BUno, edo_BDos, edo_BTres;
        private Timer tiempo;
        private Cliente miCliente;
        private visualizaCalentador visCalentador;

        public Controlador()
        {
            fechaHoraActual = DateTime.Now;
            tiempo = new Timer();
            tiempo.Tick+=tiempo_Tick;
            tiempo.Interval = 1000;
            tiempo.Enabled = true;
            tiempo.Start();
            edo_Encendido = edo_BUno = edo_BDos = edo_BTres = false;
            miCliente = new Cliente();
            visCalentador = new visualizaCalentador();
        }

        private void tiempo_Tick(object sender, EventArgs e)
        {
            fechaHoraActual = DateTime.Now;
            setDia();
            setHora();
            setMinuto();
            img_Puntos.Visible = !img_Puntos.Visible;
        }

        internal void setManejadores()
        {
            setDia();
            setHora();
            setMinuto();
            img_Left.MouseDown += img_Left_MouseDown;
            img_Left.MouseUp += img_Left_MouseUp;
            img_Right.MouseDown += img_Right_MouseDown;
            img_Right.MouseUp += img_Right_MouseUp;
            img_Up.MouseDown += img_Up_MouseDown;
            img_Up.MouseUp += img_Up_MouseUp;
            img_Down.MouseDown += img_Down_MouseDown;
            img_Down.MouseUp += img_Down_MouseUp;
            img_Power.MouseClick += img_Power_MouseClick;
            img_Uno.MouseClick += img_Uno_MouseClick;
            img_Dos.MouseClick += img_Dos_MouseClick;
            img_Tres.MouseClick += img_Tres_MouseClick;
        }

        void img_Tres_MouseClick(object sender, MouseEventArgs e)
        {
            Bitmap bmp = img_Tres.BackgroundImage as Bitmap;

            if (bmp != null && !bmp.GetPixel(e.X * 32 / 60, e.Y * 32 / 60).Equals(Color.FromArgb(232, 233, 234)))
            {
                if (!edo_BUno && !edo_BDos && (edo_BTres = !edo_BTres))
                {
                    img_Tres.BackgroundImage = Datiotec.Properties.Resources.botonTresON;
                }
                else
                    img_Tres.BackgroundImage = Datiotec.Properties.Resources.botonTres;
            }
        }

        void img_Dos_MouseClick(object sender, MouseEventArgs e)
        {
            Bitmap bmp = img_Dos.BackgroundImage as Bitmap;

            if (bmp != null && !bmp.GetPixel(e.X * 32 / 60, e.Y * 32 / 60).Equals(Color.FromArgb(232, 233, 234)))
            {
                if (!edo_BUno && !edo_BTres && (edo_BDos = !edo_BDos))
                {
                    img_Dos.BackgroundImage = Datiotec.Properties.Resources.botonDosON;
                }
                else
                    img_Dos.BackgroundImage = Datiotec.Properties.Resources.botonDos;
            }
        }

        void img_Uno_MouseClick(object sender, MouseEventArgs e)
        {
            Bitmap bmp = img_Uno.BackgroundImage as Bitmap;

            if (bmp != null && !bmp.GetPixel(e.X * 32 / 60, e.Y * 32 / 60).Equals(Color.FromArgb(232, 233, 234)))
            {
                if (!edo_BDos && !edo_BTres && (edo_BUno = !edo_BUno))
                {
                    img_Uno.BackgroundImage = Datiotec.Properties.Resources.botonUnoON;
                }
                else
                    img_Uno.BackgroundImage = Datiotec.Properties.Resources.botonUno;
            }
        }

        void img_Power_MouseClick(object sender, MouseEventArgs e)
        {
            System.Threading.Thread hilo;
            
            Bitmap bmp = img_Power.BackgroundImage as Bitmap;
            if (bmp != null && !bmp.GetPixel(e.X / 2, e.Y / 2).Equals(Color.FromArgb(232, 233, 234)))
            {
                if ((edo_Encendido = !edo_Encendido))
                {
                    hilo = new System.Threading.Thread(miCliente.encender);
                    hilo.Start();
                    visCalentador.encender();
                    try
                    {
                        visCalentador.Show();
                    }
                    catch (ObjectDisposedException) { 
                    visCalentador = new visualizaCalentador();
                    visCalentador.encender();
                    visCalentador.Show();
                    }
                    img_Power.BackgroundImage = Datiotec.Properties.Resources.powerON;
                }
                else
                {
                    hilo = new System.Threading.Thread(miCliente.apagar);
                    hilo.Start();

                    visCalentador.apagar();
                    try
                    {
                        visCalentador.Show();
                    }
                    catch (ObjectDisposedException)
                    {
                        visCalentador = new visualizaCalentador();
                        visCalentador.apagar();
                        visCalentador.Show();
                    }
                    img_Power.BackgroundImage = Datiotec.Properties.Resources.power;
                }
            }
        }

        private void setDia()
        {
            switch (fechaHoraActual.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    img_Dia.BackgroundImage = Datiotec.Properties.Resources.lunes;
                    break;
                case DayOfWeek.Tuesday:
                    img_Dia.BackgroundImage = Datiotec.Properties.Resources.martes;
                    break;
                case DayOfWeek.Wednesday:
                    img_Dia.BackgroundImage = Datiotec.Properties.Resources.miercoles;
                    break;
                case DayOfWeek.Thursday:
                    img_Dia.BackgroundImage = Datiotec.Properties.Resources.jueves;
                    break;
                case DayOfWeek.Friday:
                    img_Dia.BackgroundImage = Datiotec.Properties.Resources.viernes;
                    break;
                case DayOfWeek.Saturday:
                    img_Dia.BackgroundImage = Datiotec.Properties.Resources.sabado;
                    break;
                case DayOfWeek.Sunday:
                    img_Dia.BackgroundImage = Datiotec.Properties.Resources.domingo;
                    break;
            }
        }

        private void setHora()
        {
            switch (fechaHoraActual.Hour)
            {
                case 0:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    break;
                case 1:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    break;
                case 2:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    break;
                case 3:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay3;
                    break;
                case 4:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay4;
                    break;
                case 5:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay5;
                    break;
                case 6:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay6;
                    break;
                case 7:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay7;
                    break;
                case 8:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay8;
                    break;
                case 9:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay9;
                    break;
                case 10:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    break;
                case 11:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    break;
                case 12:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    break;
                case 13:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay3;
                    break;
                case 14:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay4;
                    break;
                case 15:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay5;
                    break;
                case 16:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay6;
                    break;
                case 17:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay7;
                    break;
                case 18:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay8;
                    break;
                case 19:
                    
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay9;
                    break;
                case 20:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    break;
                case 21:
                    
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    break;
                case 22:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    break;
                case 23:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay3;
                    break;
                case 24:
                    img_DisplayH1.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    img_DisplayH2.BackgroundImage = Datiotec.Properties.Resources.dislpay4;
                    break;
            }
        }

        private void setMinuto()
        {
            switch (fechaHoraActual.Minute / 10)
            {
                case 0:
                    img_DisplayM1.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    break;
                case 1:
                    img_DisplayM1.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    break;
                case 2:
                    img_DisplayM1.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    break;
                case 3:
                    img_DisplayM1.BackgroundImage = Datiotec.Properties.Resources.dislpay3;
                    break;
                case 4:
                    img_DisplayM1.BackgroundImage = Datiotec.Properties.Resources.dislpay4;
                    break;
                case 5:
                    img_DisplayM1.BackgroundImage = Datiotec.Properties.Resources.dislpay5;
                    break;
                case 6:
                    img_DisplayM1.BackgroundImage = Datiotec.Properties.Resources.dislpay6;
                    break;
            }

            switch (fechaHoraActual.Minute % 10)
            {
                case 0:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay0;
                    break;
                case 1:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay1;
                    break;
                case 2:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay2;
                    break;
                case 3:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay3;
                    break;
                case 4:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay4;
                    break;
                case 5:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay5;
                    break;
                case 6:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay6;
                    break;
                case 7:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay7;
                    break;
                case 8:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay8;
                    break;
                case 9:
                    img_DisplayM2.BackgroundImage = Datiotec.Properties.Resources.dislpay9;
                    break;
            }
        }

        void img_Down_MouseUp(object sender, MouseEventArgs e)
        {
            img_Down.BackgroundImage = Datiotec.Properties.Resources.down;
        }

        void img_Down_MouseDown(object sender, MouseEventArgs e)
        {
            Bitmap bmp = img_Down.BackgroundImage as Bitmap;
            if (bmp != null && !bmp.GetPixel(e.X * 32 / 53, e.Y * 60 / 100).Equals(Color.FromArgb(232, 233, 234)))
                img_Down.BackgroundImage = Datiotec.Properties.Resources.downON;
        }

        void img_Up_MouseUp(object sender, MouseEventArgs e)
        {
            img_Up.BackgroundImage = Datiotec.Properties.Resources.up;
        }

        void img_Up_MouseDown(object sender, MouseEventArgs e)
        {
            Bitmap bmp = img_Up.BackgroundImage as Bitmap;
            if(bmp != null && !bmp.GetPixel(e.X * 32 / 53, e.Y * 60 / 100).Equals(Color.FromArgb(232,233,234)))
                img_Up.BackgroundImage = Datiotec.Properties.Resources.upON;
        }

        void img_Right_MouseUp(object sender, MouseEventArgs e)
        {
            img_Right.BackgroundImage = Datiotec.Properties.Resources.right;
        }

        void img_Right_MouseDown(object sender, MouseEventArgs e)
        {
            Bitmap bmp = img_Right.BackgroundImage as Bitmap;
            if (bmp != null && !bmp.GetPixel(e.X * 60 / 100, e.Y * 32 / 53).Equals(Color.FromArgb(232,233,234)))
                img_Right.BackgroundImage = Datiotec.Properties.Resources.rightON;
        }

        void img_Left_MouseUp(object sender, MouseEventArgs e)
        {
            img_Left.BackgroundImage = Datiotec.Properties.Resources.left;
        }

        void img_Left_MouseDown(object sender, MouseEventArgs e)
        {
            Bitmap bmp = img_Left.BackgroundImage as Bitmap;
            if (bmp != null && !bmp.GetPixel(e.X * 60 / 100, e.Y * 32 / 53).Equals(Color.FromArgb(232, 233, 234)))
                img_Left.BackgroundImage = Datiotec.Properties.Resources.leftON;
        }
    }
}
